package cn.ddy.handlers;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 * @author ddy
 * @create 2020-08-12 16:26:00
 */
public class DateTypeHandler extends BaseTypeHandler<Date> {
    //将java类型转换成数据库类型
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException {
        long time = date.getTime();
        preparedStatement.setLong(i,time);
    }
    //将数据库类型转换成java类型
    //String 参数  要转换的字段名称
    //ResultSet 查询出的结果集
    @Override
    public Date getNullableResult(ResultSet resultSet, String s) throws SQLException {
        //将结果集中的数据类型(long)转换成Date类型返回
        long aLong = resultSet.getLong(s);
        java.sql.Date date = new java.sql.Date(aLong);
        return date;
    }
    //将数据库类型转换成java类型
    @Override
    public Date getNullableResult(ResultSet resultSet, int i) throws SQLException {
        //将结果集中的数据类型(long)转换成Date类型返回
        long aLong = resultSet.getLong(i);
        java.sql.Date date = new java.sql.Date(aLong);
        return date;
    }
    //将数据库类型转换成java类型
    @Override
    public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        long aLong = callableStatement.getLong(i);
        java.sql.Date date = new java.sql.Date(aLong);
        return date;
    }
}
